import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-10-20
 * Time: 22:19
 */
public class MybitSet {
    public byte[] elem;
    public int usedSize;
    public MybitSet(){
        elem = new byte[1];
    }
    public MybitSet(int n){
        elem = new byte[n / 8 + 1];
    }
    public void set(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException("负数无法直接哈希，数组下标越界");
        }
        int arrIndex = val / 8;
        //如果哈希位置越界了，那么就扩容数组
        if(arrIndex > elem.length - 1){
            elem = Arrays.copyOf(elem, arrIndex + 1);
        }
        int bitIndex = val % 8;
        //不能进行异或，如果bitIndex位置本身就是1就变成0了
        elem[arrIndex] |= (1 << bitIndex);
        usedSize++;
    }
    public boolean get(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException("负数无法直接哈希，数组下标越界");
        }
        int arrIndex = val / 8;
        int bitIndex = val % 8;
       if((elem[arrIndex] & (1 << bitIndex)) != 0){
           return true;
       }
       return false;
    }
    public void reSet(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException("负数无法直接哈希，数组下标越界");
        }
        int arrIndex = val / 8;
        int bitIndex = val % 8;
        elem[arrIndex] &= ~(1 << bitIndex);
        usedSize--;
    }
    public int getUsedSize(){
        return usedSize;
    }
}
